<?php

class BreadCrumb extends CWidget {

    public $crumbs = array();
    public $newCrumb = array();
    public $delimiter = ' <i class="next_cate"></i> ';
    public $hideCrumbsOnHome = true;
    public $firstCrumb = array('Home' => array('name' => 'Home', 'url' => array('site/index')));
    public $excludeCrumbs = array('Login');
    public $term = '';
    public $truncatedCrumb = array('Truncated' => array('name' => '...'));

    public function run() {
        $this->crumbs = $this->firstCrumb;
        //nếu là danh mục
        $page_slug = '';
        $action = Yii::app()->controller->action->id;


        if (@$_GET['slug']) {

            $page_slug = Object::model()->cache(3600)->find("object_slug=:action", array(':action' => $_GET['slug']));
            if ($page_slug) {
                $action = 'product';
            }
        }
//        if(@$_GET['block']){
//            $block=PageBlock::model()->cache(3600)->find("block_id=:block",array(':block'=>$_GET['block']));
//        }
        if (@$_GET['id']) {
            $object = Object::model()->find("object_id=:id", array(':id' => @$_GET['id']));
        }
//        
        switch ($action) {
            case 'product':

                $this->crumbs = array_merge($this->crumbs, array($page_slug['name'] => array('name' => $page_slug['object_name'], 'url' => array('page/render', 'slug' => $page_slug['slug']))));
                break;
            case 'detailtour': //die('pass');
                $term = Term::model()->getTermNameByObject($object['object_id']);
                if (!empty($term)) {
                    $this->crumbs = array_merge($this->crumbs, array($term['name'] => array('name' => $term['name'], 'url' => array('page/listtourdestination', 'destination_slug' => $term['slug']))));
                }
                if (!empty($object)) {
                    $this->crumbs = array_merge($this->crumbs, array($object['object_name'] => array('name' => display($object['object_name'], 50, 50), 'url' => '')));
                }
                break;
            case 'detailcar': //die('pass');
//                $term=Term::model()->getTermNameByObject($object['object_id']);
//                if(!empty($term)){
//                    $this->crumbs=  array_merge($this->crumbs,array($term['name']=>array('name'=>$term['name'],'url'=>array('page/listtourdestination','destination_slug'=>$term['slug']))));
//                }

                if (!empty($object)) {
                    $this->crumbs = array_merge($this->crumbs, array('name' => array('name' => Yii::t('text', 'Transport'), 'url' => array('page/render', 'slug' => 'transport'))));
                    $this->crumbs = array_merge($this->crumbs, array($object['object_name'] => array('name' => display($object['object_name'], 50, 50), 'url' => '')));
                }
                break;
            case 'dattour':
                $page_slug = Page::model()->find("slug=:action and lang=:lang", array(':action' => 'book-tour', ':lang' => getLanguage()));
                if (!empty($page_slug)) {
                    $this->crumbs = array_merge($this->crumbs, array($page_slug['title'] => array('name' => $page_slug['title'], 'url' => '')));
                }
                if (!empty($object)) {
                    $this->crumbs = array_merge($this->crumbs, array($object['object_name'] => array('name' => display($object['object_name'], 50, 50), 'url' => '')));
                }
                break;
            case 'detailnews':
                $term = Term::model()->getTermNameByObject($object['object_id']);
                if (@$_GET['block']) {
                    $page_id = PageBlock::model()->find("block_id=:block_id", array(':block_id' => @$_GET['block']));
                    if (!empty($page_id)) {
                        $page = Page::model()->find("page_id=:page_id", array(':page_id' => $page_id['page_id']));
                    }
                }
                if (!empty($page)) {
                    $this->crumbs = array_merge($this->crumbs, array($page['title'] => array('name' => $page['title'], 'url' => array('page/render', 'slug' => $page['slug']))));
                }
                if (!empty($object)) {
                    $this->crumbs = array_merge($this->crumbs, array($object['object_name'] => array('name' => display($object['object_name'], 50, 50), 'url' => '')));
                }
                break;
            case 'detailattension':
                $term = Term::model()->getTermNameByObject($object['object_id']);
                if (@$_GET['block']) {
                    $page_id = PageBlock::model()->find("block_id=:block_id", array(':block_id' => @$_GET['block']));
                    if (!empty($page_id)) {
                        $page = Page::model()->find("page_id=:page_id", array(':page_id' => $page_id['page_id']));
                    }
                }
                if (!empty($page)) {
                    $this->crumbs = array_merge($this->crumbs, array($page['title'] => array('name' => $page['title'], 'url' => array('page/render', 'slug' => $page['slug']))));
                }
                if (!empty($object)) {
                    $this->crumbs = array_merge($this->crumbs, array($object['object_name'] => array('name' => display($object['object_name'], 50, 50), 'url' => '')));
                }
                break;
            case 'listtourdestination':
                $term_parent = Term::model()->find('slug=:myslug', array(':myslug' => $_GET['destination_slug']));
                if (!empty($term_parent)) {
                    $this->crumbs = array_merge($this->crumbs, array($term_parent['name'] => array('name' => $term_parent['name'], 'url' => '')));
                }
                break;
            case 'introductiondestination':
                $page_slug = Page::model()->find("slug=:action and lang=:lang", array(':action' => 'list-destination', ':lang' => getLanguage()));
                if (!empty($page_slug)) {
                    $this->crumbs = array_merge($this->crumbs, array($page_slug['title'] => array('name' => $page_slug['title'], 'url' => array('page/render', 'slug' => $page_slug['slug']))));
                }
                //$term_parent=Term::model()->find('slug=:myslug',array(':myslug'=>$_GET['obj_slug']));
                //$this->crumbs=  array_merge($this->crumbs,array($term_parent['name']=>array('name'=>display(Yii::t('text','Introduction to the')." ".$term_parent['name'],50,50),'url'=>'')));
                $obj_id = $_GET['obj_id'];
                $obj = Object::model()->findByPk($obj_id);
                $this->crumbs = array_merge($this->crumbs, array($_GET['obj_slug'] => array('name' => display($obj['object_title'], 50, 50), 'url' => '')));
                break;
            default :

                $this->crumbs = array_merge($this->crumbs, array($this->newCrumb['name'] => array('name' => $this->newCrumb['name'], 'url' => '')));
                break;
        }

        $this->render('breadCrumb');
    }

}

?>